﻿using System;
using System.Data;
using System.Collections.Generic;
using eTMS.Controllers.TietHocService;
namespace eTMS.Controllers{
public class TietHocControllerBase : CommonController{
	public TietHocControllerBase()
		: base()
	{	
		this.dao = new TietHocServiceClient();
	}
	~TietHocControllerBase(){
		dao.Close();
	}
	private DataTable _Table;
	private TietHocEntity oTietHoc;	
	public TietHocEntity TietHocObject{
		get{
			return oTietHoc;
		}
		set{
			oTietHoc=value;
		}
	}
	protected TietHocServiceClient dao;
	public DataTable Table{
		get{
			this.LoadAllDSTietHoc(false);			
			return _Table;
		}
	}
	public void LoadAllDSTietHoc(bool clearCache){
		bool bForce=clearCache;
		if(_Table==null)
		{
			bForce=true;
		}
		if(!bForce)return;
		DataSet ds=dao.SelectAllDS();
		if(ds.Tables.Count>0){
			_Table=ds.Tables[0];
			this.AddIndexToTable(_Table);
		}
	}	
	public void AddIndexToTable(){
		this.AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		this.ResetTableIndex(_Table);
	}
	public bool AddNew(){
		if(oTietHoc==null)return false;
		bool result=true;
		try{
			oTietHoc = dao.SaveRet(oTietHoc);
		}
		catch{
			result=false;
		}
		if(result){
			_ListTietHoc.Add(oTietHoc);
		}
		return result;
	}
	public bool Update(){
		if(oTietHoc==null)return false;
		return dao.Update(oTietHoc);
	}
	public bool Delete(){
		if(oTietHoc==null)return false;
		bool retvalue = dao.Delete(oTietHoc);
		if(retvalue){			
			_ListTietHoc.Remove(oTietHoc);
		}
		return retvalue;
	}
	public bool DeleteByID(Int32 TietHoc_ID){
		bool retvalue = dao.DeleteByID(TietHoc_ID);		
		if(retvalue){
			TietHocEntity oDeleted=FindTietHoc(TietHoc_ID);
			if(oDeleted!=null){
				_ListTietHoc.Remove(oDeleted);
			}
		}
		return retvalue;
	}
	public void ResetObject(){
		this.oTietHoc = new TietHocEntity();
	}
	//Check if exists MaTietHoc
	public bool CheckExistsMaTietHoc(object value,int CaHoc_ID){	
		return dao.SelectAllFilter("MaTietHoc='"+value.ToString()+"' AND CaHoc_ID="+CaHoc_ID.ToString()).Length>0;
    }    
    //Copy data from data row in grid to object
    public TietHocEntity MapDataRowToObject(DataRow row){
		TietHocEntity o = new TietHocEntity();
		 o.MaTietHoc=Convert.ToString(row["MaTietHoc"]);
		 o.TenTietHoc=Convert.ToString(row["TenTietHoc"]);
		 o.GhiChu=Convert.ToString(row["GhiChu"]);
		 if(!row.IsNull("ThuTu"))
			o.ThuTu=ConvertInt(row["ThuTu"]);
		 if(!row.IsNull("CaHoc_ID"))
			o.CaHoc_ID=(Int32)(row["CaHoc_ID"]);
		 if(!row.IsNull("TietHoc_ID"))
		    o.TietHoc_ID=(Int32)(row["TietHoc_ID"]);
		 return o;
    }
	//Copy data from object to data row in grid    
    public void MapObjectToDataRow(TietHocEntity o,DataRow row){
		row["MaTietHoc"]=o.MaTietHoc;
		row["TenTietHoc"]=o.TenTietHoc;
		row["GhiChu"]=o.GhiChu;
		row["ThuTu"]=o.ThuTu;
		row["CaHoc_ID"]=o.CaHoc_ID;
		row["TietHoc_ID"]=o.TietHoc_ID;
    }
    public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.TietHocObject,row);
    }
	public void LoadObject(Int32 TietHoc_ID){
		TietHocEntity oLoad=FindTietHoc(TietHoc_ID);
		if(oLoad==null){
			oTietHoc=this.LoadTietHoc(TietHoc_ID);		
			bool bNotIsNull= true;
			 bNotIsNull&=oTietHoc.IsNullTietHoc_ID;
			if(!bNotIsNull){
				this.ListTietHoc.Add(oTietHoc);
			}
		}
		else{
			oTietHoc=oLoad;
		}
	}
	#region CachedList TietHoc
	private static List<TietHocEntity> _ListTietHoc=new List<TietHocEntity>();
    public List<TietHocEntity>ListTietHoc{
		get{
			return _ListTietHoc;
		}
	}
	public static List<TietHocEntity>ListTietHocStatic{
		get{
			return _ListTietHoc;
		}
	}
	public TietHocEntity FindTietHoc(Int32 TietHoc_ID){
		TietHocEntity oResult=this.ListTietHoc.Find(delegate(TietHocEntity oFind){
			bool result=true; 
			result&=oFind.TietHoc_ID.Equals(TietHoc_ID);
			return result;
		 });
		return oResult;
	}
	public static void LoadAllTietHoc(bool clearCache){
		if(_ListTietHoc.Count==0)
			clearCache=true;
		if(!clearCache)return;		
		TietHocServiceClient dao = new TietHocServiceClient();
		_ListTietHoc=new List<TietHocEntity>(dao.SelectAll());
		dao.Close();
	}
	public TietHocEntity LoadTietHoc(Int32 TietHoc_ID){			
		TietHocEntity o=dao.SelectOne(TietHoc_ID);		
		return o;
	}
	#endregion
}
}
